import { Quiz, MistakeQuestion } from "../../types";

export const generateQuiz = async (topic: string): Promise<Quiz> => {
    const response = await fetch("/api/quiz/generate", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({ topic }),
    });

    if (!response.ok) {
        throw new Error(`Failed to generate quiz: ${response.statusText}`);
    }

    const result = await response.json();

    if (result.error) {
        throw new Error(result.error);
    }

    return result.data;
};

export const generateQuizFromMistakes = async (questions: MistakeQuestion[]): Promise<Quiz> => {
    const response = await fetch("/api/quiz/generate", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({ questions }),
    });

    if (!response.ok) {
        throw new Error(`Failed to generate quiz: ${response.statusText}`);
    }

    const result = await response.json();

    if (result.error) {
        throw new Error(result.error);
    }

    return result.data;
};
